package rs.arm.meetingcaller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.widget.TextView;

public class CalendarReader extends Activity {

	private Cursor mCursor = null;
	private static final String[] COLS = new String[] {
			CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART,
			CalendarContract.Events.DTEND, CalendarContract.Events.DURATION,
			CalendarContract.Events.RRULE, CalendarContract.Events.RDATE };
	private static final String DATE_TIME_FORMAT = "yyyy MMM dd, HH:mm:ss";
	private TextView calendarTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reader_layout);
		long today = Calendar.getInstance().getTimeInMillis();
		long todayminus24 = today - 12 * 3600 * 1000;
		long todayplus24 = today + 12 * 3600 * 1000;
		mCursor = getContentResolver().query(
				CalendarContract.Events.CONTENT_URI, COLS,
				"(?<DTSTART and DTEND<?) or (RRULE <> ?)",
				new String[] { "" + todayminus24, "" + todayplus24, "" }, null);
		calendarTextView = (TextView) findViewById(R.id.calendarInput);
		setCalendarText();
	}

	private void setCalendarText() {
		if (!mCursor.moveToFirst()) {
			calendarTextView.setText("There is no events in calendar");
			return;
		}
		StringBuffer sb = new StringBuffer();
		int titleInt = mCursor.getColumnIndex(COLS[0]);
		int startInt = mCursor.getColumnIndex(COLS[1]);
		int endInt = mCursor.getColumnIndex(COLS[2]);
		int durr = mCursor.getColumnIndex(COLS[3]);
		int rrule = mCursor.getColumnIndex(COLS[4]);
		int rdate = mCursor.getColumnIndex(COLS[5]);
		do {
			String strTitle = mCursor.getString(titleInt);
			String strStart = mCursor.getString(startInt);
			String strEnd = mCursor.getString(endInt);
			String strDurr = mCursor.getString(durr);
			String strRrule = mCursor.getString(rrule);
			String strRdate = mCursor.getString(rdate);
			sb.append("Title:").append(strTitle).append("\n");
			sb.append("Start:").append(getDateTimeStr(strStart)).append("\n");
			if (strEnd != null)
				sb.append("End:").append(getDateTimeStr(strEnd)).append("\n");
			if (strDurr != null) {
				sb.append("Duration:").append(strDurr).append("\n");
			}
			if (strRrule == null)
				sb.append("One time event").append("\n");
			else {
				sb.append(recurance(strRrule)).append("\n");
			}
			sb.append("-------------------------------\n");
		} while (mCursor.moveToNext());
		calendarTextView.setText(sb.toString());
	}

	public String getDateTimeStr(String time) {
		if (time == null)
			return "yyyy MMM dd, HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
		Date strTime = new Date(Long.parseLong(time));
		return sdf.format(strTime);
	}

	public long getDuration(String duration) {
		String d = duration.substring(duration.indexOf("P"));
		return 0;
	}

	public String recurance(String s) {
		String[] req = s.split(";");
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < req.length; i++) {
			if (req[i].contains("FREQ")) {
				if (req[i].contains("WEEKLY")) {
					sb.append("Weekly meeting\n");
				}
				if (req[i].contains("MONTHLY")) {
					sb.append("Monthly meeting\n");
				}
				if (req[i].contains("DAILY")) {
					sb.append("Daily meeting\n");
				}
				if (req[i].contains("YEARLY")) {
					sb.append("Yearly meeting\n");
				}
			}
			if (req[i].contains("BYDAY")) {
				boolean comma = false;
				sb.append("Meeting take a place every:");
				if (req[i].contains("MO")) {
					sb.append("Monday");
					comma = true;
				}
				if (req[i].contains("TU")) {
					if (comma)
						sb.append(",");
					sb.append("Tuesday");
					comma = true;
				}
				if (req[i].contains("WE")) {
					if (comma)
						sb.append(",");
					sb.append("Wednesday");
					comma = true;
				}
				if (req[i].contains("TH")) {
					if (comma)
						sb.append(",");
					sb.append("Thursday");
					comma = true;
				}
				if (req[i].contains("FR")) {
					if (comma)
						sb.append(",");
					sb.append("Friday");
					comma = true;
				}
				if (req[i].contains("SA")) {
					if (comma)
						sb.append(",");
					sb.append("Saturday");
					comma = true;
				}
				if (req[i].contains("SU")) {
					if (comma)
						sb.append(",");
					sb.append("Sunday");
					comma = true;
				}
			}
		}
		return sb.toString();
	}

	// FREQ=WEEKLY;WKST=SU;BYDAY=MO,TU,WE,TH,FR

	/*
	 * dur-value = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
	 * 
	 * dur-date = dur-day [dur-time] dur-time = "T" (dur-hour / dur-minute /
	 * dur-second) dur-week = 1*DIGIT "W" dur-hour = 1*DIGIT "H" [dur-minute]
	 * dur-minute = 1*DIGIT "M" [dur-second] dur-second = 1*DIGIT "S" dur-day =
	 * 1*DIGIT "D"
	 */

}
